package com.jqbd.service.impl;

import com.alibaba.excel.EasyExcel;
import com.jqbd.mapper.AdminMapper;
import com.jqbd.pojo.Admin;
import com.jqbd.pojo.User;
import com.jqbd.service.AdminService;
import com.jqbd.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author jh
 * Created by Enzo Cotter on 2022/11/11.
 */
@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) {

        return adminMapper.login(username,password);
    }

    @Override
    public List<Admin> findAll() {
        return adminMapper.findAll();
    }

    @Override
    public int  add(Admin admin) {
        return adminMapper.add(admin);
    }

    @Override
    public int deleteById(Integer adminId) {
       return adminMapper.deleteById(adminId);
    }

    @Override
    public int deleteByIds(Integer[] deleteByIds) {
      return   adminMapper.deleteByIds(deleteByIds);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.update(admin);
    }

    @Override
    public void  writeExcel(Integer[] ids, HttpServletResponse response) {
        //判断
        if(ids.length > 0){
            //查询数据库
            List<Admin> patientList = adminMapper.findByIds(ids);
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String fileName = "admin-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, Admin.class).sheet("管理员数据").doWrite(patientList);
        }else {
            //如果没有选择性导出数据，那么就导出所有
            //查询数据库
            List<Admin> patientList = adminMapper.findAllNoparm();
            //格式化当前时间
            String currTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

            //通过下载的方式导出Excel文件
            String fileName = "admin-" + currTime + ".xlsx";
            //设置响应头
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
            //通过流的方式写入
            ServletOutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //导出Excel
            EasyExcel.write(outputStream, Admin.class).sheet("管理员数据").doWrite(patientList);
        }
    }
}
